package com.baby.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.baby.mall.product.entity.CategoryEntity;
import com.baby.mall.product.service.CategoryService;
import com.baby.common.utils.PageUtils;
import com.baby.common.utils.R;



/**
 * 商品三级分类
 *
 * @author qinglongzhai
 * @email qinglongzhai@gmail.com
 * @date 2021-03-15 10:47:09
 */
@Api(tags = "商品三级分类")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所以分类以及子分类，以树形结构组装
     */
    @ApiOperation("查出所有分类以及子分类，以树形结构组装")
    @RequestMapping(value = "/list/tree",method = RequestMethod.GET)
    //@RequiresPermissions("product:category:list")
    public R list(){
        List<CategoryEntity> entities=categoryService.listWithTree();

        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询分类")
    @RequestMapping(value = "/info/{catId}",method = RequestMethod.GET)
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存分类
     */
    @ApiOperation("保存分类")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }
    /**
     * 修改
     */
    @ApiOperation("修改排序")
    @RequestMapping(value = "/update/sort",method = RequestMethod.POST)
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改分类")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
		//categoryService.removeByIds(Arrays.asList(catIds));

		categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
